﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using QLNhaHangTiecCuoi.Models;

namespace QLNhaHangTiecCuoi.Controllers
{
    public class NhanViensController : Controller
    {
        private QLNhaHangTCEntities db = new QLNhaHangTCEntities();

        // GET: NhanViens
        public async Task<ActionResult> Index()
        {
            var nhanViens = db.NhanViens.Include(n => n.ChucVu);
            return View(await nhanViens.ToListAsync());
        }

        // GET: NhanViens/Details/5
        public async Task<ActionResult> Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            NhanVien nhanVien = await db.NhanViens.FindAsync(id);
            if (nhanVien == null)
            {
                return HttpNotFound();
            }
            return View(nhanVien);
        }

        // GET: NhanViens/Create
        public ActionResult Create()
        {
            ViewBag.Chonchucvu = GetChucVu();
            //ViewBag.MaCV = new SelectList(db.ChucVus, "MaCV", "TenCV");
            return View();
        }
        public string CreateMaNV()
        {
            var item = db.NhanViens.OrderByDescending(i => i.MaNV).FirstOrDefault();
            string ma;
            if(item==null){ma="NV0000";}
            else ma= item.MaNV;
            int so = Convert.ToInt32(ma.Substring(2, 4));
            so++;
            string MaNV = "NV" + so.ToString("0000");
            return MaNV;
        }


        // POST: NhanViens/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "TenNV,Email,DiaChi,SDT,MaCV,GhiChu")] NhanVien nhanVien)
        {
            NhanVien NV = new NhanVien()
            {
                MaNV =  CreateMaNV(),
                TenNV = nhanVien.TenNV,
                Email = nhanVien.Email,
                DiaChi = nhanVien.DiaChi,
                SDT = nhanVien.SDT,
                MaCV = nhanVien.MaCV,
                GhiChu = nhanVien.GhiChu
            };
            if (ModelState.IsValid)
            {
                db.NhanViens.Add(NV);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.Chonchucvu = GetChucVu();
           // ViewBag.MaCV = new SelectList(db.ChucVus, "MaCV", "TenCV", nhanVien.MaCV);
            return View(nhanVien);
        }

        // GET: NhanViens/Edit/5
        public async Task<ActionResult> Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            NhanVien nhanVien = await db.NhanViens.FindAsync(id);
            if (nhanVien == null)
            {
                return HttpNotFound();
            }
            ViewBag.Chonchucvu = GetChucVu();
            //ViewBag.MaCV = new SelectList(db.ChucVus, "MaCV", "TenCV", nhanVien.MaCV);
            return View(nhanVien);
        }

        // POST: NhanViens/Edit/5
       
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "MaNV,TenNV,Email,DiaChi,SDT,MaCV,GhiChu")] NhanVien nhanVien)
        {
            if (ModelState.IsValid)
            {
                db.Entry(nhanVien).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.Chonchucvu = GetChucVu();
            //ViewBag.MaCV = new SelectList(db.ChucVus, "MaCV", "TenCV", nhanVien.MaCV);
            return View(nhanVien);
        }

        // GET: NhanViens/Delete/5
        public async Task<ActionResult> Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            NhanVien nhanVien = await db.NhanViens.FindAsync(id);
            if (nhanVien == null)
            {
                return HttpNotFound();
            }
            return View(nhanVien);
        }

        // POST: NhanViens/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(string id)
        {
            NhanVien nhanVien = await db.NhanViens.FindAsync(id);
            db.NhanViens.Remove(nhanVien);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        private List<SelectListItem> GetChucVu()
        {
            var x = (from n in db.ChucVus select n);
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var item in x)
            {
                items.Add(new SelectListItem { Text = item.TenCV, Value = item.MaCV });
            }
            return items;
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
